package com.shz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shz.pojo.Follow;
import com.shz.pojo.User;
import com.shz.service.FollowService;
import com.shz.service.UserService;
import com.shz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shz
 * @since 2024-09-02
 */
@Api(tags = "用户关注模块")
@RestController
@RequestMapping("/follow")
public class FollowController {
    @Resource
    private FollowService followService;

    /**
     *
     * 关注是true，已关注是false
     */
    @ApiOperation(value = "关注该用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "所关注的用户的id", required = true),
            @ApiImplicitParam(name = "isFollow", value = "（发送请求后的状态）显示关注为true,显示已关注是false", required = true),
    })
    @PutMapping("/{id}/{isFollow}")
    public Boolean follow(@PathVariable("id") Integer followUserId, @PathVariable("isFollow") Boolean isFollow) {
        Integer uid = UserUtil.getCurrentUser().getId();
        if(!isFollow){
            Follow follow=new Follow();
            follow.setUid(uid);
            follow.setFuid(followUserId);
            return followService.save(follow);

        }else{
            return followService.remove(new QueryWrapper<Follow>().eq("uid", uid).eq("fuid", followUserId));
        }
    }
    @ApiOperation(value = "我是否关注该用户（状态）")
    @ApiImplicitParam(name = "fuid", value = "所关注的用户的id", required = true)
    @GetMapping("/isFollow/{fuid}")
    public boolean isFollow(@PathVariable("fuid") Integer followUserId) {
        Integer uid = UserUtil.getCurrentUser().getId();
        Follow one = followService.query().eq("uid", uid).eq("fuid", followUserId).one();
        return one != null;
    }




    @Resource
    UserService userService;

    @ApiOperation(value = "他的关注列表")
    @ApiImplicitParam(name = "uid", value = "所关注的用户的id", required = true)
    @GetMapping("/follow/{uid}")
    public List<User> getThisUserFollow(@PathVariable("uid") Integer uid){
        List<User> list = userService.query().eq("uid", uid).list();
        for (User user : list) {
            user.setPassword("");
        }
        return list;
    }


}

